package com.egao.common.manage.school;

import cn.hutool.core.util.StrUtil;
import com.egao.common.component.elasticsearch.entity.vo.StudentPushMsgVO;
import com.egao.common.component.elasticsearch.service.ElasticSearchService;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.push.constants.PushConstants;
import com.egao.common.module.system.entity.College;
import com.egao.common.module.system.entity.PushViewRecord;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.service.CollegeService;
import com.egao.common.module.system.service.PushViewRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author lym
 * @date 2022/9/27 14:53
 */
@Api(tags = "推送信息管理")
@RestController
@RequestMapping("${api}")
public class SchoolPushMsgController extends BaseController {

    @Autowired
    private ElasticSearchService elasticSearchService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private PushViewRecordService pushViewRecordService;

    @PreAuthorize("hasAuthority('school')")
    @ApiOperation("列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = false, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/school/push-msg/page")
    public PageResult<StudentPushMsgVO> list(@RequestParam(name="page",defaultValue="0") Integer page, Integer limit, StudentPushMsgVO params) throws IOException {
        User loginUser = getLoginUser();
        // 学院
        if (!StrUtil.isBlank(loginUser.getCollegeCode())) {
            College college = collegeService.getByCode(loginUser.getCollegeCode());
            if (college != null) {
                params.setCollegeName(college.getName());
            }
        }

        PushViewRecord pushViewRecord = new PushViewRecord();
        pushViewRecord.setUserId(getLoginUserId());
        pushViewRecordService.save(pushViewRecord);

        return elasticSearchService.listPushMsg(params, page, limit);
    }
}
